import { ClassSerializerInterceptor, ValidationPipe, VersioningType } from '@nestjs/common';
import { NestFactory, Reflector } from '@nestjs/core';
import { AppModule } from './app.module';
import { AllExceptionsFilter } from './exception/all-exceptions.filter';
import { HttpExceptionFilter } from './exception/http-exception.filter';
import { PrismaExceptionFilter } from './exception/prisma-exception-filter';
import { TransformInterceptor } from './interceptor/transform.interceptor';

async function bootstrap() {
  const app = await NestFactory.create(AppModule);
  // fastify上传文件有问题
  // const app = await NestFactory.create<NestFastifyApplication>(AppModule, new FastifyAdapter());
  app.useGlobalPipes(new ValidationPipe({ transform: true, transformOptions: { enableImplicitConversion: true } }));
  app.setGlobalPrefix('api');
  app.useGlobalInterceptors(new TransformInterceptor(), new ClassSerializerInterceptor(app.get(Reflector)));
  app.enableVersioning({
    type: VersioningType.URI,
    defaultVersion: '1',
  });
  // useGlobalFilters 优先级是最后一个是最先执行，所以捕获全部的放第一个，会最后执行
  // 如果只有三个全局过滤器，顺序 useGlobalFilters(3,2,1)
  app.useGlobalFilters(new AllExceptionsFilter(), new HttpExceptionFilter(), new PrismaExceptionFilter());

  await app.listen(3000);
}
bootstrap();
